package com.ccp.dev.qbdms.util;

import com.alibaba.fastjson.JSONObject;
import com.ccp.dev.qbdms.model.WQbdFlightJc;
import com.ccp.dev.qbdms.model.WQbdFlightJczb;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import org.apache.commons.compress.utils.Lists;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;


/**
 * 发送HTTP请求
 *
 * @author :milihao
 */
public class HttpSendUtil {


	/**
	 * 共用接口请求方法
	 *
	 * @param url 请求路径
	 * @param method 请求方法
	 * @param params 请求参数
	 * @return ResponseEntity
	 */
	public static ResponseEntity<JSONObject> httpRestClient(String url, HttpMethod
			method, Map<String, Object> params) {
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
		requestFactory.setConnectTimeout(15 * 1000);
		requestFactory.setReadTimeout(1200 * 1000);
		requestFactory.setBufferRequestBody(false);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		RestTemplate client = new RestTemplate(requestFactory);
		HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(params, headers);
		return client.exchange(url, method, requestEntity, JSONObject.class);
	}

	public static void main(String[] args) {
		Map<String, Object> paramMap = Maps.newHashMap();
		WQbdFlightJc wQbdFlightJc = new WQbdFlightJc();
		wQbdFlightJc.setXhmc("aaa");
		wQbdFlightJc.setStatus("bbb");
		List<WQbdFlightJczb> wQbdFlightJczbList = Lists.newArrayList();
		WQbdFlightJczb wQbdFlightJczb;
		for (int i = 0; i < 3; i++) {
			wQbdFlightJczb = new WQbdFlightJczb();
			wQbdFlightJczb.setId(String.valueOf(i));
			wQbdFlightJczbList.add(wQbdFlightJczb);
		}
		wQbdFlightJc.setwQbdFlightJczbList(wQbdFlightJczbList);
		paramMap.put("param", wQbdFlightJc);
		System.out.println(
				httpRestClient("http://localhost:8080/test/httpClient/test", HttpMethod.POST, paramMap));
	}
}
